package controllers;

import java.util.Calendar;
import java.util.Timer;

import com.mysql.jdbc.EscapeTokenizer;

/**
 * This class is responsible for the scheduled tasks operation.
 * It starts a timer that starts the jobs every day.
 * @author Alon Shahaf
 * @Data 12.01.2013
 */

public class JobsManager 
{
	private Calendar cycle_cal;
	private long period;
	private boolean running;
	private DBController db_ref = null;
	private EchoServer echo_ref = null;
	private IntSysClock clk_ref = null;
	
	private Timer jobCheckLateBorrows = null;
	private Timer jobCheckOrders = null;
	
	/**
	 * Sets up the job manager.
	 * @author Alon Shahaf
	 * @Data 12.01.2013
	 * @param h Hour to run the job.
	 * @param m Minutes to run the job.
	 * @param s Seconds to run the job.
	 * @param ms Milliseconds to run the job.
	 * @param period time to wait between each run.
	 */
	public JobsManager(int h,int m,int s,int ms, long peroid)
	{
		this.cycle_cal = Calendar.getInstance();
		this.cycle_cal.set(Calendar.HOUR, h);
		this.cycle_cal.set(Calendar.MINUTE, m);
		this.cycle_cal.set(Calendar.SECOND, s);
		this.cycle_cal.set(Calendar.MILLISECOND, ms);
		this.period = peroid;
		this.running = false;
	}
	
	/**
	 * Starts the jobs cycle.
	 * @author Alon Shahaf
	 * @Data 12.01.2013
	 */	
	public void startCycle()
	{
		if ((db_ref == null) || (db_ref.isClosed()) || (echo_ref == null) || (echo_ref.isClosed()) || (clk_ref == null))
		{
			System.out.println("DailyJobsManager (startCycle) - Unable to start - Some of the components is invalid.");
			return;
		}
		running = true;
		System.out.println("DailyJobsManager (startCycle) - Starting cycle.");
		
		// Start Job #1 - JobCheckBorrows
        jobCheckLateBorrows = new Timer();
        jobCheckLateBorrows.schedule(new JobCheckBorrows(db_ref,echo_ref,clk_ref),
	               0L,        // initial date
	               period);  // subsequent rate

        // Start Job #2 - JobCheckOrders
        jobCheckOrders = new Timer();
        jobCheckOrders.schedule(new JobCheckOrders(db_ref,echo_ref,clk_ref),
	               0L,        // initial date
	               period);  // subsequent rate

	}

	/**
	 * Stops the jobs cycle.
	 * @author Alon Shahaf
	 * @Data 12.01.2013
	 */
	public void stopCycle()
	{
		try
		{
			jobCheckLateBorrows.cancel();
			jobCheckOrders.cancel();
			running = false;
			System.out.println("DailyJobsManager (stopCycle) - Cycle stopped.");
		}
		catch(Exception e)
		{
			System.out.println("DailyJobsManager (stopCycle) - Jobs failed to stop.");
		}
	}
	
	/**
	 * Checks if the job manager is running.
	 * @author Alon Shahaf
	 * @Data 12.01.2013
	 * @return true if the job manager is running, false otherwise.
	 */
	public boolean isRunning()
	{
		return running;
	}
	
	public void setDB(DBController db)
	{
		this.db_ref = db;
	}
	
	public void setEcho(EchoServer echo)
	{
		this.echo_ref = echo;
	}
	
	public void setClock(IntSysClock clk)
	{
		this.clk_ref = clk;
	}
}
